package cn.hnh.service.impl;

import cn.hnh.constrant.ResultInfo;
import cn.hnh.mapper.ArchivesMapper;
import cn.hnh.mapper.FriendsMapper;
import cn.hnh.service.ArchivesService;
import cn.hnh.utils.ListToMapByYear;
import cn.hnh.vo.ArchivesArticleVo;
import cn.hnh.vo.ArchivesVo;
import cn.hnh.vo.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author HuangNianHong
 * @Time 2021/1/2 21:56
 */
@Service
public class ArchivesServiceImpl implements ArchivesService {
    @Autowired
    private ArchivesMapper archivesMapper;
    @Override
    public Result<ArchivesVo> getArchivesVo(Integer pagenum) {
        PageHelper.startPage(pagenum, 10);
        List<ArchivesArticleVo> list = archivesMapper.getArchivesVo();
        PageInfo<ArchivesArticleVo> page = new PageInfo<>(list);
        if(page.getPages()!=0){
            ArchivesVo archivesVo = new ArchivesVo();
            archivesVo.setPageNum(page.getPageNum());
            archivesVo.setTotalpage(page.getPages());
            archivesVo.setNextPageNum(page.getNextPage());
            archivesVo.setPrePageNum(page.getPrePage());
            archivesVo.setHasNextPage(page.isHasNextPage());
            archivesVo.setHasPreviousPage(page.isHasPreviousPage());
            archivesVo.setArchivesCount(archivesMapper.getArchiverCount());
            archivesVo.setArchivesMap(ListToMapByYear.transfer(page.getList()));
            return new Result<>(ResultInfo.SUCCESS_CODE,ResultInfo.SUCCESS_MESSAGE,archivesVo);
        }
        return new Result<>(ResultInfo.NOTFOUND_CODE,ResultInfo.NOTFOUND_MESSAGE);
    }
}
